
/**
 * Created by 蔡健 on 2017/8/10.
 */
//sprite撞击测试
function hitTestRectangle(r1, r2) {

	//Define the variables we'll need to calculate
	var hit, combinedHalfWidths, combinedHalfHeights, vx, vy;

	//hit will determine whether there's a collision
	hit = false;

	//Find the center points of each sprite
	r1.centerX = r1.x + r1.width / 2;
	r1.centerY = r1.y + r1.height / 2;
	r2.centerX = r2.x + r2.width / 2;
	r2.centerY = r2.y + r2.height / 2;

	//Find the half-widths and half-heights of each sprite
	r1.halfWidth = r1.width / 2;
	r1.halfHeight = r1.height / 2;
	r2.halfWidth = r2.width / 2;
	r2.halfHeight = r2.height / 2;

	//Calculate the distance vector between the sprites
	vx = r1.centerX - r2.centerX;
	vy = r1.centerY - r2.centerY;

	//Figure out the combined half-widths and half-heights
	combinedHalfWidths = r1.halfWidth + r2.halfWidth;
	combinedHalfHeights = r1.halfHeight + r2.halfHeight;

	//Check for a collision on the x axis
	if (Math.abs(vx) < combinedHalfWidths) {

		//A collision might be occuring. Check for a collision on the y axis
		if (Math.abs(vy) < combinedHalfHeights) {

			//There's definitely a collision happening
			hit = true;
		} else {

			//There's no collision on the y axis
			hit = false;
		}
	} else {

		//There's no collision on the x axis
		hit = false;
	}

	//`hit` will be either `true` or `false`
	return hit;
};
//判断容器边界
function contain(sprite, container) {

	var collision = undefined;

	//Left
	if (sprite.x < container.x) {
		sprite.x = container.x;
		collision = "left";
	}

	//Top
	if (sprite.y < container.y) {
		sprite.y = container.y;
		collision = "top";
	}

	//Right
	if (sprite.x + sprite.width > container.width) {
		sprite.x = container.width - sprite.width;
		collision = "right";
	}

	//Bottom
	if (sprite.y + sprite.height > container.height) {
		sprite.y = container.height - sprite.height;
		collision = "bottom";
	}

	//Return the `collision` value
	return collision;
}
//监听键盘事件
function keyboard(keyCode,keyMode) {
	if(keyMode===undefined) keyMode = "ctrl";

	var key = {};
	key.code = keyCode;
	key.isDown = false;
	key.isUp = true;
	key.press = undefined;
	key.release = undefined;
	//The `downHandler`
	key.downHandler = function(event) {
		if (event.keyCode === key.code) {
			if (key.isUp && key.press) key.press();
			key.isDown = true;
			key.isUp = false;
		}
		if(keyMode=="ctrl"){
			onKeyDown(event);
		}
	};

	//The `upHandler`
	key.upHandler = function(event) {
		if (event.keyCode === key.code) {
			if (key.isDown && key.release) key.release();
			key.isDown = false;
			key.isUp = true;
		}
		event.preventDefault();
	};

	//Attach event listeners
	document.addEventListener(
		"keydown", key.downHandler.bind(key)
	);
	document.addEventListener(
		"keyup", key.upHandler.bind(key)
	);
	return key;
}


/**
 * 从 file 域获取 本地图片 url
 */
function getFileUrl(sourceId) {
    var url;
    if (navigator.userAgent.indexOf("MSIE")>=1) { // IE
        url = document.getElementById(sourceId).value;
    } else if(navigator.userAgent.indexOf("Firefox")>0) { // Firefox
        url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
    } else if(navigator.userAgent.indexOf("Chrome")>0) { // Chrome
        url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
    }
    return url;
}


//获取 Sprite 的 左上角坐标和 宽 高
function getStandardShape(shape){
    var x,y,width,height;
    if(shape.width < 0 && shape.height >0){
        x = shape.x + shape.width;
        y = shape.y;
        width = - shape.width;
        height = shape.height;
    }else if(shape.height < 0 && shape.width > 0 ){
        x = shape.x
        y = shape.y + shape.height;
        width = shape.width;
        height = - shape.height;
    }else if(shape.width < 0 && shape.height < 0 ){
        x = shape.x + shape.width;
        y = shape.y + shape.height;
        width = - shape.width;
        height = - shape.height;
    }else{
        x = shape.x;
        y = shape.y;
        width = shape.width;
        height = shape.height;
    }
    return{x:x,y:y,width:width,height:height};
}

//获取裁剪的范围参数（sprite及其坐标和宽，高）
function getCutArea(shape,children){
    var x,y,width,height;

    //获取 方框  左上 和 右下 坐标
    var cl_x,cl_y,cr_x,cr_y;
    cl_x = shape.x;
    cl_y = shape.y;
    cr_x = shape.x+shape.width;
    cr_y = shape.y+shape.height;

    var spt,sspt,sl_x,sl_y,sr_x,sr_y,rs;

    for(var i = 1;i<children.length ;i++){
        spt = children[i];
        if(!(spt instanceof PIXI.Sprite)){continue;}
        if(spt.type == 'dragPoint' || spt.type == 'shade'){continue;}
        x = 0; y = 0; width = 0 ;height = 0; sspt = null;

        //获取 Sprite 左上 和 右下 坐标
        sl_x = spt.x- spt.anchor.x * spt.width;
        sl_y = spt.y- spt.anchor.y * spt.height;

        sr_x = spt.x + (1-spt.anchor.x) * spt.width;
        sr_y = spt.y + (1-spt.anchor.y) * spt.height;
        // 方框完全包含 Sprite
        if(cl_x < sl_x && cl_y < sl_y &&  cr_x > sr_x && cr_y > sr_y){
            x = 0;
            y = 0;
            width = spt.width;
            height = spt.height;
        }else if(cr_x > sl_x && cr_x < sr_x && cr_y > sl_y && cr_y < sr_y){ //方框 右下角 在 Sprite 内
            if(cl_x < sl_x && cl_y < sl_y){ //左上角 在Sprite 外 且 左上角 在 Sprite 左斜上方
                x = 0;
                y = 0;
                width = cr_x - sl_x;
                height = cr_y-sl_y;
            }else if(cl_x> sl_x && cl_y<sl_y){ //左上角 在Sprite 外 且 左上角 在 Sprite 上方
                x = cl_x-sl_x;
                y = 0;
                width = cr_x - cl_x;
                height = cr_y - sl_y;
            }else if(cl_x < sl_x && cl_y > sl_y){ //左上角 在Sprite 外 且 左上角 在 Sprite 左方
                x = 0;
                y = cl_y - sl_y;
                width = cr_x - sl_x;
                height = cr_y - cl_y;
            }else if(cl_x>sl_x && cl_y > sl_y){ //左上角 在Sprite 内 （方框整个都在 Sprite 内）
                x = cl_x - sl_x;
                y = cl_y - sl_y;
                width = cr_x - cl_x;
                height = cr_y - cl_y;
            }
        }else if(cl_x > sl_x && cl_x < sr_x && cr_y > sl_y && cr_y < sr_y){ //方框 左下角 在 Sprite 内
            if(cr_x > sr_x && cl_y < sl_y){ //右上角 在Sprite 外 且 右上角 在 Sprite 右斜上方
                x = cl_x - sl_x;
                y = 0;
                width = sr_x - cl_x;
                height = cr_y-sl_y;
            }else if(cr_x < sr_x && cl_y<sl_y){ //右上角 在Sprite 外 且 右上角 在 Sprite 上方
                x = cl_x-sl_x;
                y = 0;
                width = cr_x - cl_x;
                height = cr_y - sl_y;
            }else if(cr_x > sr_x && cl_y > sl_y){ //右上角 在Sprite 外 且 右上角 在 Sprite 右方
                x = cl_x - sl_x;
                y = cl_y - sl_y;
                width = sr_x - cl_x;
                height = cr_y - cl_y;
            }else if(cl_x>sl_x && cl_y > sl_y){ //右上角 在Sprite 内 （方框整个都在 Sprite 内）
                x = cl_x - sl_x;
                y = cl_y - sl_y;
                width = cr_x - cl_x;
                height = cr_y - cl_y;
            }
        }else if(cl_x < sr_x && cl_x > sl_x && cl_y < sr_y && cl_y > sl_y){ //方框 左上角 在 Sprite 内
            if(cr_x > sr_x && cr_y > sr_y){ //右下角 在Sprite 外 且 右下角 在 Sprite 右斜下方
                x = cl_x - sl_x;
                y = cl_y - sl_y;
                width = sr_x - cl_x;
                height = sr_y-cl_y;
            }else if(cr_x < sr_x && sr_y < cr_y){ //右下角 在Sprite 外 且 右下角 在 Sprite 下方
                x = cl_x-sl_x;
                y = cl_y-sl_y;
                width = cr_x - cl_x;
                height = sr_y - cl_y;
            }else if(cr_x > sr_x && cr_y < sr_y){ //右下角 在Sprite 外 且 右下角 在 Sprite 右方
                x = cl_x - sl_x;
                y = cl_y - sl_y;
                width = sr_x - cl_x;
                height = cr_y - cl_y;
            }else if(cl_x>sl_x && cl_y > sl_y){ //右下角 在Sprite 内 （方框整个都在 Sprite 内）
                x = cl_x - sl_x;
                y = cl_y - sl_y;
                width = cr_x - cl_x;
                height = cr_y - cl_y;
            }
        }else if(cr_x > sl_x && cr_x < sr_x && cl_y > sl_y && cl_y < sr_y){ //方框 右上角 在 Sprite 内
            if(cl_x < sl_x && cr_y > sr_y){ //左下角 在Sprite 外 且 左下角 在 Sprite 左斜下方
                x = 0;
                y = cl_y-sl_y;
                width = cr_x - sl_x;
                height = sr_y-cl_y;
            }else if(cr_x < sr_x && cl_y<sl_y){ //左下角 在Sprite 外 且 左下角 在 Sprite 下方
                x = cl_x - sl_x;
                y = cl_y - sl_y;
                width = cr_x - cl_x;
                height = sr_y - cl_y;
            }else if(cr_x > sr_x && cl_y > sl_y){ //左下角 在Sprite 外 且 左下角 在 Sprite 左方
                x = 0;
                y = cl_y - sl_y;
                width = cr_x - sl_x;
                height = cr_y - cl_y;
            }else if(cl_x>sl_x && cl_y > sl_y){ //左下角 在Sprite 内 （方框整个都在 Sprite 内）
                x = cl_x - sl_x;
                y = cl_y - sl_y;
                width = cr_x - cl_x;
                height = cr_y - cl_y;
            }
        }else if(cl_y < sl_y && cr_y > sr_y){ //方框 竖向切割 Sprite
            if(cl_x < sl_x && sl_x < cr_x){ //左边 sprint 在 方框 内
                x = 0;
                y = 0;
                width = cr_x - sl_x;
                height = sr_y-sl_y;
            }else if(cr_x < sr_x && sl_x<cl_x){ //中间 sprint 在 方框 内
                x = cl_x - sl_x;
                y = 0;
                width = cr_x - cl_x;
                height = sr_y - sl_y;
            }else if(cr_x > sr_x && cl_x < sr_x){ //右边 sprint 在 方框内
                x = cl_x - sl_x;
                y = 0;
                width = sr_x - cl_x;
                height = sr_y - sl_y;
            }
        }else if(cl_x < sl_x && cr_x > sr_x){ //方框 横向切割 Sprite
            if(cl_y < sl_y && cr_y > sl_y){ //上边 sprint 在 方框 内
                x = 0;
                y = 0;
                width = sr_x - sl_x;
                height = cr_y-sl_y;
            }else if(cl_y > sl_y && cr_y < sr_y){ //中间 sprint 在 方框 内
                x = 0;
                y = cl_y - sl_y;
                width = sr_x - sl_x;
                height = cr_y - cl_y;
            }else if(cr_y > sr_y && cl_y < sr_y){ //下边 sprint 在 方框内
                x = 0;
                y = cl_y - sl_y;
                width = sr_x - sl_x;
                height = sr_y - cl_y;
            }
        }

        if(width != 0 && height != 0){
            sspt = spt;
            rs = {x:x,y:y,width:width,height:height,spt:sspt};
            break;
        }
    }
    return rs;
}